package com.tjp.eight.ui.fragment.jifenshop;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.jifenshop.goodsdetail.ActivityGoodsDetail;
import com.tjp.eight.utils.DipPxSpUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/13.
 */
public class JFShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private View headView;
    private ArrayList<JinBShopItemModel> models;

    public JFShopAdapter(Context context) {
        this.context = context;
        models = new ArrayList<>();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new JFShopHeadViewHolder(headView);
            case 1:
                View item = LayoutInflater.from(context).inflate(R.layout.item_jf_shop, parent, false);
                return new JFShopItemViewHolder(item);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                break;
            case 1:
                int pos = position - 1;

                if (position % 2 == 0)
                    ((JFShopItemViewHolder) holder).view.setPadding(DipPxSpUtils.dip2px(context, 8), 0, DipPxSpUtils.dip2px(context, 16), 0);
                else {
                    ((JFShopItemViewHolder) holder).view.setPadding(DipPxSpUtils.dip2px(context, 16), 0, DipPxSpUtils.dip2px(context, 8), 0);
                }

                ((JFShopItemViewHolder) holder).tv_goods_price.setText("" + models.get(pos).money);
                ((JFShopItemViewHolder) holder).tv_goods_title.setText("" + models.get(pos).title);
                Glide.with(context).load(models.get(pos).thumb).error(R.drawable.icon_fail).into(((JFShopItemViewHolder) holder).iv_goods_thumb);
                final String gid = models.get(pos).gid;
                ((JFShopItemViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 进入商品详情 gid
                        Intent intent = new Intent(context, ActivityGoodsDetail.class);
                        intent.putExtra("gid", gid);
                        context.startActivity(intent);
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return headView == null ? models.size() : models.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && headView != null) {
            return 0;
        }
        return 1;
    }

    public void setHeadView(View headView) {
        this.headView = headView;
    }

    public View getHeadView() {
        return headView;
    }

    public void addData(ArrayList<JinBShopItemModel> list) {
        if (list == null || list.isEmpty()) return;
        for (JinBShopItemModel item : list) {
            models.add(item);
        }
        notifyDataSetChanged();
    }

    class JFShopHeadViewHolder extends RecyclerView.ViewHolder {

        public JFShopHeadViewHolder(View itemView) {
            super(itemView);
        }
    }

    class JFShopItemViewHolder extends RecyclerView.ViewHolder {
        View view;
        ImageView iv_goods_thumb;
        TextView tv_goods_title;
        TextView tv_goods_price;

        public JFShopItemViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            iv_goods_thumb = (ImageView) itemView.findViewById(R.id.iv_goods_thumb);
            tv_goods_title = (TextView) itemView.findViewById(R.id.tv_goods_title);
            tv_goods_price = (TextView) itemView.findViewById(R.id.tv_goods_price);
        }
    }
}
